<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/7/2
 * Time: 10:00
 */
namespace app\orders\controller;

use app\common\ErrorCodes;
use think\Lang;
class Fitting extends Base{


    public function index(){
//        $this->authVerify();
        $field = 'fid,fitting,pid,create_time,is_ciu';
        $where = '';
        $fittings = $this->fittings()->join($where);
        return $this->show($fittings);
        /*$this->page($fittings);
        $this->assign('fittings', $fittings);
        $this->asgPrincs();
        return $this->fetch("fit/index");*/
    }

    /**
     * 添加action
     */
    public function save(){
        $this->authVerify();
        $fittings = input('post.');
        $fittings['create_time'] = date('Y-m-d H:i:s', time());
        $validate = $this->validate($fittings, 'Fits');
        //var_dump($validate);exit();
        if(true !== $validate){
            return $this->show(array(), ErrorCodes::$FAIL, " $validate ");
//            return $this->error(" $validate ");
        }
        $result = $this->fittings()->add($fittings, '');
        if(!$result){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('add fail'));
//            return $this->error(Lang::get('add fail'));
        }
        return $this->show($fittings, ErrorCodes::$SUCCESS, Lang::get('add success'));
//        return $this->success(Lang::get('add success'), 'Fitting/index');
    }

    /**
     * json数据
     */
    public function one(){
        $fid = input('param.fid');
        $field = 'fid,fitting,pid,create_time,is_ciu';
        $where = array('fid'=>$fid);
        $data = $this->fittings()->select($field, $where);
        return $this->show($data);
    }

    /**
     * 更新action
     */
    public function esave(){
        $this->authVerify();
        $fid = input('param.fid');
        $where = array('fid'=>$fid);
        $find = $this->fittings()->findById($where);
        if(!$find){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind fitting'));
//            return $this->error(Lang::get('unfind fitting'));
        }
        $fittings = input('post.');
        $validate = $this->validate($fittings, 'Fits');
        //var_dump($validate);exit();
        if(true !== $validate){
            return $this->show(array(), ErrorCodes::$FAIL, " $validate ");
//            return $this->error(" $validate ");
        }
        $result = $this->fittings()->update($fittings, $where);
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('edit fail'));
//            return $this->error(Lang::get('edit fail'));
        }
        return $this->show($fittings, ErrorCodes::$SUCCESS, Lang::get('edit success'));
//        return $this->success(Lang::get('edit success'), 'Fitting/index');
    }

    /**
     * 删除action
     */
    public function del(){
        $this->authVerify();
        $fid = input('param.fid');
        $where = array('fid'=>$fid);
        $find = $this->fittings()->findById($where);
        if(!$find){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind fitting'));
//            return $this->error(Lang::get('unfind fitting'));
        }
        $result = $this->fittings()->del($where);
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('del fail'));
//            return $this->error(Lang::get('del fail'));
        }
        return $this->show($find, ErrorCodes::$SUCCESS, Lang::get('del success'));
//        return $this->success(Lang::get('del success'), 'Fitting/index');
    }

    /**
     * 简单的模糊搜索
     * @return mixed
     */
    public function search(){
        $search = input('param.search');
        $data = $this->fittings()->searchLike($search);
        return $this->show($data);
        /*$this->page($data);
        $this->assign('fittings', $data);
        $this->asgPrincs();
        return $this->fetch('fit/index');*/
    }

    /**
     * 重组-二维数组
     * @param $fittings
     * @return mixed
     */
    private function resetFittings($fittings){
        $len = count($fittings);
        if($len > 0){
            for($i=0;$i<$len;$i++){
                $fittings[$i] = $this->resetFitting($fittings[$i]);
            }
        }
        return $fittings;
    }

    /**
     * 重组-一维数组
     * @param $fitting
     * @return mixed
     */
    private function resetFitting($fitting){
        $len = count($fitting);
        if($len > 0){
            for($i=0;$i<$len;$i++){
                $pid = $fitting['pid'];
                $find = $this->principles()->findById(array('pid'=>$pid));
                $fitting['product_principle'] = $find['product_principle'];
            }
        }else{
            $fitting['product_principle'] = '';
        }
        return $fitting;
    }
}